using FireStarterModeller.BO;
using Habanero.BO;
using Habanero.Util;
using NUnit.Framework;

namespace FireStarterModeller.Test.BO
{
    [TestFixture]
    public class TestBusinessObjectProperty :TestBase
    {
        [SetUp]
        public override void SetupTest()
        {
            //Runs every time that any testmethod is executed
            base.SetupTest();
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            //Code that is executed before any test is run in this class. If multiple tests
            // are executed then it will still only be called once.
            base.SetupFixture();
        }

        [TearDown]
        public override void TearDownTest()
        {
            //runs every time any testmethod is complete
            base.TearDownTest();
        }

        [Test]
        public void Test_InsertNewDMProperty()
        {
            //---------------Set up test pack-------------------
            DMProperty dmProperty = TestUtilsShared.CreateUnsavedValidDMProperty();

            //---------------Assert Precondition----------------
            Assert.IsTrue(dmProperty.Status.IsNew);

            //---------------Execute Test ----------------------
            dmProperty.Save();

            //---------------Test Result -----------------------
            Assert.IsFalse(dmProperty.Status.IsNew);
        }

        [Test]
        public void Test_InsertNewDMProperty_Invalid()
        {
            //---------------Set up test pack-------------------
            DMProperty dmProperty = new DMProperty();

            //---------------Assert Precondition----------------
            Assert.IsTrue(dmProperty.Status.IsNew);

            //---------------Execute Test ----------------------
            try
            {
                dmProperty.Save();
                Assert.Fail("expected Err");
            }
            //---------------Test Result -----------------------
            catch (BusObjectInAnInvalidStateException ex)
            {
                StringAssert.Contains("Class ID' is a compulsory field and has no value", ex.Message);
                StringAssert.Contains("Property Name' is a compulsory field and has no value", ex.Message);
            }
            Assert.IsTrue(dmProperty.Status.IsNew);
        }

        [Test]
        public void Test_DeleteDMProperty()
        {
            //---------------Set up test pack-------------------
            DMProperty dmProperty = TestUtilsShared.CreateSavedDMProperty(TestUtilsShared.CreateSavedDMClass());

            //---------------Assert Precondition----------------
            Assert.IsFalse(dmProperty.Status.IsNew);
            string message;
            Assert.IsTrue(dmProperty.IsDeletable(out message), "Should be deletable : " + message);

            //---------------Execute Test ----------------------
            dmProperty.MarkForDelete();
            dmProperty.Save();

            //---------------Test Result -----------------------  
            Assert.IsTrue(dmProperty.Status.IsNew);
            Assert.IsTrue(dmProperty.Status.IsDeleted);
        }

        [Test]
        public void Test_UpdateDMProperty()
        {
            //---------------Set up test pack-------------------
            DMProperty dmProperty = TestUtilsShared.CreateSavedDMProperty(TestUtilsShared.CreateSavedDMClass());

            //---------------Assert Precondition----------------
            Assert.IsFalse(dmProperty.Status.IsNew);
            string message;
            Assert.IsTrue(dmProperty.IsEditable(out message));

            //---------------Execute Test ----------------------
            dmProperty.PropertyName = TestUtilsShared.GetRandomString();
            //dmProperty.PropertyType = TestUtilsShared.GetRandomString();
            dmProperty.DisplayName = TestUtilsShared.GetRandomString();
            //dmProperty.PropertyAssembly = TestUtilsShared.GetRandomString();
            dmProperty.Generated = true;

            dmProperty.Save();

            //---------------Test Result -----------------------
            Assert.IsFalse(dmProperty.Status.IsNew);
        }

        [Test]
        public void Test_LoadDMProperty()
        {
            //---------------Set up test pack-------------------
            DMProperty dmProperty = TestUtilsShared.CreateSavedDMProperty(TestUtilsShared.CreateSavedDMClass());
            BusinessObjectManager.Instance.ClearLoadedObjects();

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            DMProperty loadedDM = Broker.GetBusinessObject<DMProperty>(dmProperty.ID);

            //---------------Test Result -----------------------  
            Assert.IsFalse(loadedDM.Status.IsNew);
            Assert.AreEqual(loadedDM.ID, dmProperty.ID);
            //TODO: When not using memory db Assert.AreNotSame(loadedDM, businessObjectInfo);
        }

        [Test]
        public void TestLink_PropertyNameToMappedDBColumnName()
        {
            //---------------Set up test pack-------------------
            DMProperty newBO = new DMProperty();
            //---------------Execute Test ----------------------
            const string testautocomplete = "TestAutoComplete";
            newBO.PropertyName = testautocomplete;
            //---------------Test Result -----------------------
            Assert.AreEqual(testautocomplete, newBO.MappedDBColumnName);
        }

        [Test]
        public void TestLink_PropertyNameToDisplayName()
        {
            //---------------Set up test pack-------------------
            DMProperty newBO = new DMProperty();
            //---------------Execute Test ----------------------
            const string testautocomplete = "TestAutoComplete";
            newBO.PropertyName = testautocomplete;
            //---------------Test Result -----------------------
            Assert.AreEqual(StringUtilities.DelimitPascalCase(testautocomplete, " "), newBO.DisplayName);
        }

    }
}
